package com.freewater.javabase.demo.mutithread.daemon;

// 在守护线程中，编写代码要注意：守护线程不能持有任何需要关闭的资源，例如打开文件等，因为虚拟机退出时，守护线程没有任何机会来关闭文件，这会导致数据丢失
public class DaemonThread {

    public static void main(String[] args) throws InterruptedException {
        HelloThread helloThread = new HelloThread();
        helloThread.setDaemon(true);
        helloThread.start();

        Thread.sleep(8);
        System.out.println("end");
    }

    public static class HelloThread extends Thread {
        @Override
        public void run() {
            int n = 0;
            while (!interrupted()) {
                System.out.println(n++ + " hello");
            }
        }
    }
}
